package com.bop.dehong.decaifu.fragment;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.base.dehong.http.RequestCallBack;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseFragment;
import com.bop.dehong.base.DeHongApp;
import com.bop.dehong.config.Config;
import com.bop.dehong.decaifu.adapter.RecordListAdapter;
import com.bop.dehong.info.entity.BidrecordEntity;
import com.bop.dehong.http.model.RecordModel;
import com.bop.dehong.widget.PullToRefreshView;
import com.bop.dehong.widget.PullToRefreshView.OnFooterRefreshListener;
import com.bop.dehong.widget.PullToRefreshView.OnHeaderRefreshListener;

public class Bidrecord extends BaseFragment implements OnHeaderRefreshListener, OnFooterRefreshListener {
	private ListView bidrecord_list;
	private PullToRefreshView bidrecord_list_pull_refresh_view;
	private RecordListAdapter recordListAdapter;	
	private RecordModel recordModel;
	private int mPage = 1;
	private int totalpage;
	private String id, uid;
	private BidrecordEntity entity;
	
	@Override
	protected void initTop() {
		mPageName = getClass().getName();
		
	}

	@Override
	protected void initUI() {
		id = getActivity().getIntent().getExtras().getString(Config.CAT_ID);
		bidrecord_list = getView(R.id.bidrecord_list);
		bidrecord_list_pull_refresh_view = getView(R.id.bidrecord_list_pull_refresh_view);
		bidrecord_list_pull_refresh_view.setOnHeaderRefreshListener(this);
		bidrecord_list_pull_refresh_view.setOnFooterRefreshListener(this);
		if (recordListAdapter == null)
		{
			recordListAdapter = new RecordListAdapter(Bidrecord.this.getActivity());
		}
		bidrecord_list.setAdapter(recordListAdapter);
	}

	@Override
	public void initData() {
			if (DeHongApp.mLoginEntity != null) {
				uid = DeHongApp.mLoginEntity.getUid();
			}else {
				ToastUtils.show(getActivity(), "还未登录，请先登录再查询投标记录！");
				return;
			}
			if (entity == null)
			{
				showProgressDialog();
				if (recordModel == null) {
					recordModel = new RecordModel(getActivity(), callback);
				}
				recordModel.doNet(id, uid, mPage + "");
			}
	}

	RequestCallBack<BidrecordEntity> callback = new RequestCallBack<BidrecordEntity>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}
		
		public void onSuccess(BidrecordEntity info) {
			if (entity == null && recordListAdapter.size() > 0) {
				recordListAdapter.clear();
			}
			entity = info;
			recordListAdapter.appendToList(info.bidRecords);
			totalpage = info.totalpages;
			super.onSuccess(info);
		};
		
	};

	
	@Override
	protected int getContentViewId() {
		return R.layout.fragment_bidrecord;
	}

	@Override
	public void onHeaderRefresh(PullToRefreshView view)
	{
				mPage = 1;
				entity = null;
				initData();
				bidrecord_list_pull_refresh_view.onHeaderRefreshComplete();
	}

	@Override
	public void onFooterRefresh(PullToRefreshView view)
	{
				if (mPage < totalpage) {
					mPage ++;	
				}else {
					ToastUtils.show(getActivity(), "没有更多了!");
					bidrecord_list_pull_refresh_view.onFooterRefreshComplete();
					return;
				}
				if (recordModel == null) {
					recordModel = new RecordModel(getActivity(), callback);
				}
				recordModel.doNet(id, uid, mPage + "");
				bidrecord_list_pull_refresh_view.onFooterRefreshComplete();
	}

	@Override
	public void onClick(View v)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		// TODO Auto-generated method stub
		
	}

}
